/*
 * func_declarations.h
 *
 *  Created on: Jan 17, 2014
 *      Author: Rohit
 */

#ifndef FUNC_DECLARATIONS_H_
#define FUNC_DECLARATIONS_H_
#include "includes.h"

/*
 * func_declarations.h
 *
 *  Created on: Jan 17, 2014
 *      Author: Rohit
 */


void set_N_flag(void);
void clear_N_flag(void);
void set_Z_flag(void);
void clear_Z_flag(void);
void set_C_flag(void);
void clear_C_flag(void);
void set_V_flag(void);
void clear_V_flag(void);
unsigned short get_N_flag(void);
unsigned short get_Z_flag(void);
unsigned short get_V_flag(void);
unsigned short get_C_flag(void);
void update_Z_flag(unsigned short data);
void update_N_flag(int mnemonic,unsigned short data);
void update_V_flag(int mnemonic,unsigned short operand_1, unsigned short operand_2, unsigned short result);
void update_C_flag(int mnemonic,unsigned short operand_1, unsigned short operand_2, unsigned short result, int result_32_bit);

unsigned short pop();
void push(unsigned short data);
unsigned short get_Stack_Pointer();
void set_Stack_Pointer(unsigned short data);
unsigned short get_Program_Counter();
void set_Program_Counter(unsigned short data);

unsigned short read_from_memory(unsigned short address, unsigned short access_type);
void write_in_memory(unsigned short address, unsigned short data, unsigned short access_type);
MemValidStatus get_memory_status(unsigned short address);
void set_memory_status(unsigned short status,unsigned short address);

unsigned short get_twos_complement(unsigned short data);
void func_MOV(unsigned short address_source, unsigned short address_destination);
void func_CMP(unsigned short address_source, unsigned short address_destination);
void func_BIS(unsigned short address_source, unsigned short address_destination);
unsigned short func_ADD(unsigned short address_source, unsigned short address_destination);
void func_BIC(unsigned short address_source, unsigned short address_destination);
void func_BIT(unsigned short address_source, unsigned short address_destination);
void func_CLR(int mnemonic, unsigned short address_destination);
void func_INC(int mnemonic, unsigned short address_destination);
void func_DEC(int mnemonic, unsigned short address_destination);
void func_NEG(int mnemonic, unsigned short address_destination);
void func_TST(int mnemonic, unsigned short address_destination);
void func_COM(int mnemonic, unsigned short address_destination);
unsigned short func_ADC(unsigned short address_destination);
unsigned short func_SUB(unsigned short address_source, unsigned short address_destination);
unsigned short func_JSR(unsigned short register_address,unsigned short destination_data);
void func_RTS(unsigned short register_address);
unsigned short func_SBC(unsigned short address_destination);
void func_ROR(unsigned short destination_address);
void func_ROL(unsigned short destination_address);
void func_SWAB(unsigned short destination_address);
void func_ASR(unsigned short address_destination);
void func_ASL(unsigned short address_destination);

int func_BRANCH(int mnemonic);
void exit_on_error();
void parse_ascii(char filename[80]);

unsigned short get_operand_address(unsigned short mode, unsigned short operand_reg);
unsigned short add_mode0 (unsigned short operand_reg);
unsigned short add_mode1 (unsigned short operand_reg);
unsigned short add_mode2 (unsigned short operand_reg);
unsigned short add_mode3 (unsigned short operand_reg);
unsigned short add_mode4 (unsigned short operand_reg);
unsigned short add_mode5 (unsigned short operand_reg);
unsigned short add_mode6 (unsigned short operand_reg);
unsigned short add_mode7 (unsigned short operand_reg);


unsigned short fetch_instruction(void);
void decode_instruction(unsigned short instruction);
unsigned short check_boundary_error(unsigned short address);

void run(void);
void init(void);
void print_contents(void);
void print_mem_loc(void);

void decode_inst_group_A(unsigned short instruction);
void decode_inst_group_B(unsigned short instruction);
void decode_inst_group_C(unsigned short instruction);
void decode_inst_group_D(unsigned short instruction);
void decode_inst_group_E(unsigned short instruction);

void write_traces(int type, unsigned short address);
void write_branch_traces(char *branch_type, unsigned short instruction_PC, unsigned short target_PC, int branch_taken);

#endif /* FUNC_DECLARATIONS_H_ */
